<?php
function px_init(){
	load_plugin_textdomain('photoxhibit','wp-content/plugins/' . $px->dirName.'/lang');
}

function px_sendJSON( $jsonD = NULL, $echo = true ){
	if( $jsonD == NULL ) return false;

	if(!is_array($jsonD) && !is_a($jsonD[0], 'stdClass')){
		return '{"result":"error","errorType":"notObjTest"}';
		exit();
	}

	if (!extension_loaded('json')){
		include('JSON.php');
		$json = new JSON;
		
		if( $echo )
			echo $json->serialize($jsonD);
		else
			return $json->serialize($jsonD);
	}
	else{
		if( $echo )
			echo json_encode($jsonD);
		else
			return json_encode($jsonD);
	}
}
function px_setActions($public_query_vars){
	$public_query_vars[] = 'pxoption';
	$public_query_vars[] = 'pxsecondary';
	$public_query_vars[] = 'pxjs';
	$public_query_vars[] = 'pxgid';
	$public_query_vars[] = 'pxiid';
	$public_query_vars[] = 'curplugin_id';
	$public_query_vars[] = 'gallery_extra';
	$public_query_vars[] = 'gallery_id';
	$public_query_vars[] = 'gallery_params';
	$public_query_vars[] = 'gallery_structure';
	$public_query_vars[] = 'gallery_title';
	$public_query_vars[] = 'gallery_uselarge';
	$public_query_vars[] = 'images';
	$public_query_vars[] = 'plugin_id';
	return $public_query_vars;
}
function px_doActions(){

	if ($do_option = get_query_var('pxoption')) {
		global $wpdb, $wp_version, $wpmu_version, $wp_roles;
		global $px_view, $px_model, $px_controller, $phpSmug, $phpFlickr, $phpPicasa;

		$do_option = (string)  $do_option;
		$do_secondary = (string) get_query_var('pxsecondary');
		$get_js = (string)  get_query_var('pxjs') ;
		$get_gid = (int)  get_query_var('pxgid') ;
		$get_iid = (int)  get_query_var('pxiid') ;

		switch( $do_option){
			case 'getphpinfo':
				phpinfo();
				exit(0);
				break;
			case 'delete_image':
				$px_model->delete_image();
				break;
			case 'set':
				switch($do_secondary){
					case 'albumImage':
						$px_model->set_image_to_album($_GET);
						break;
				}
				break;
			case 'get':
				switch($do_secondary){
					case 'grabImage':
						$px_model->get_image_data($_POST);
						break;
				}
				break;
			case 'js':
				$px_view->print_js( $get_js );
				break;
			case 'css':
				$px_view->print_css( $get_gid );
				break;
			case 'doAlbum':
				$px_model->get_set_album($_GET);
				break;
			case 'optionsSet':
				$px_controller->setOptions();
				break;
			case 'smugmugAction':
				$phpSmug->login_anonymously();
				
				switch($do_secondary){
					case 'grabAlbum':
						$albums = $phpSmug->albums_get($_GET['un'], true);
						px_sendJSON($albums);
						break;
					case 'grabImages':		
						$image = $phpSmug->images_get($_GET['albumId'],$_GET['albumKey'], true);
						
						if( $phpSmug->getErrorMsg() ){
							echo $phpSmug->getErrorMsg();
						}
						else{
							px_sendJSON($image);
						}
						break;
				}
				
				$phpSmug->logout();
				break;
			case 'picasaAction':
				$phpSmug->login_anonymously();
				
				switch($do_secondary){
					case 'grabAlbum':
						$url = $px_controller->get_picasa_album_url($_GET['un']);
						$albums = $px_controller->get_json( $url );
						echo($albums);
						break;
					case 'grabImages':		
						$image = $phpSmug->images_get($_GET['albumId'],$_GET['albumKey'], true);
						
						if( $phpSmug->getErrorMsg() ){
							echo $phpSmug->getErrorMsg();
						}
						else{
							echo($image);
						}
						break;
				}
				
				break;
			case 'flickrAction':
				
				switch($do_secondary){
					case 'grabPhotosets':
						px_sendJSON( $phpFlickr->photosets_getList($_GET['un']) );
						break;
					case 'grabSearch':
						$args = array(
							'user_id' => $_GET['user_id'],
							'tags' => $_GET['tags'],
							'tag_mode' => $_GET['tag_mode'],
							'group_id' => $_GET['group_id'],
							'sort' => $_GET['sortby']
						);
						
						if( $_GET['method'] == "recent"){
							px_sendJSON( $phpFlickr->photos_getRecent() );
						}
						else{
							px_sendJSON( $phpFlickr->photos_search( $args ) );
						}
						
						break;
					case 'grabImages':		
						px_sendJSON( $phpFlickr->photosets_getPhotos($_GET['photosetId'],"license, date_upload, date_taken, owner_name, icon_server, original_format, last_update") );
						break;
				}
				
				break;
			case 'localAction':
				$sql = "SELECT 
							post_title, post_excerpt, 
							post_content, ID, 
							post_name 
						FROM $wpdb->posts 
						WHERE post_type = 'attachment' 
						AND post_mime_type LIKE '%image%'";

				$attachment_posts = $wpdb->get_results($sql, OBJECT);
			
				if( $attachment_posts ){
					foreach ($attachment_posts as $a => $v ){
						$thumbnail = wp_get_attachment_image_src($v->ID, 'thumbnail', false, true);
						$attachment_posts[$a]->thumbnail = $thumbnail;
						$intermediate = wp_get_attachment_image_src($v->ID, 'medium', false, true);
						$attachment_posts[$a]->intermediate = $intermediate;
						$attachment_posts[$a]->url = wp_get_attachment_url($v->ID);
					}
					px_sendJSON($attachment_posts);
				}
				else{
					echo '[]';
				}
				break;
				// end local action
			case 'galleryBuildAction':
				echo $px_model->processGallery($_POST);
				break;
				// end galleryBuildAction
			case 'editAttrAction':
				$px_model->update_photos();
				break;
			case 'upload':
				$px_model->upload();
				break;
		}
	
		exit(0);
	}
}

function px_favorite_actions( $actions ){
	$newactions = array(
		'admin.php?page=px_build' => array(__('Add New Gallery'), 'level_10'),
		'admin.php?page=px_manage' => array(__('Manage Galleries'), 'level_10')
		);
	
	return array_merge($actions, $newactions);
}

function px_filter_media_upload_tabs($_default_tabs){

	$new_tabs = array(
		'px_album' => __('Album')
	);
	
	if( $_GET['type'] == 'image' )
		return  array_merge($_default_tabs, $new_tabs);
	else
		return $_default_tabs;
}

/*
function media_upload_library() {
	$errors = array();
	if ( !empty($_POST) ) {
		$return = media_upload_form_handler();

		if ( is_string($return) )
			return $return;
		if ( is_array($return) )
			$errors = $return;
	}

	return wp_iframe( 'media_upload_library_form', $errors );
}
*/

?>